
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module timeCalibration
* @author: Hjava
* @description: 时间校准模块
* @since: 2018-07-18 20:39:52
*/
var session_1 = __webpack_require__(/*! _login/session */ "./src/core/login/session.ts");
var constant_1 = __webpack_require__(/*! _login/constant */ "./src/core/login/constant.ts");
var userData_1 = __webpack_require__(/*! _util/userData */ "./src/core/utils/userData.ts");
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var constant_2 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
// 发送时间校准间隔
var ADJUST_TIME = 60 * 1000;
var TimeCalibration = /** @class */ (function () {
    function TimeCalibration(connection) {
        var _this = this;
        this._connection = null;
        this._adjustHandle = null;
        this._connection = connection;
        userData_1.default.set('deltaT', 0);
        // 登录成功时开始进行时间校准
        eventEmitter_1.default.on(constant_2.LOGIN_EVENT.AUTH_SUCCESS, function () {
            clearInterval(_this._adjustHandle);
            _this._sendAdjustByServerStamp();
            _this._adjustHandle = setInterval(function () {
                _this._sendAdjustByServerStamp();
            }, ADJUST_TIME);
        });
        // 网络断开时停止时间校准
        eventEmitter_1.default.on(constant_2.LOGIN_EVENT.DISCONNECT, function () {
            clearInterval(_this._adjustHandle);
            _this._adjustHandle = null;
        });
        eventEmitter_1.default.on(constant_1.default.Login.PQRYSRVTIMESTAMPRES_URI, function (res) {
            var clientTsms = res.client_tsms;
            var serverTsms = res.server_tsms;
            var now = new Date().getTime();
            var client = parseInt(clientTsms, 10);
            var rtt = now - client;
            // 两次客户端时间大于5秒则认为校准失败，避免在校准操作中修改过时间
            if (Math.abs(rtt) > 5 * 1000) {
                return;
            }
            var t = (parseInt(serverTsms, 10) - (now + client) / 2);
            userData_1.default.set('deltaT', Math.floor(t));
        });
    }
    TimeCalibration.prototype._sendAdjustByServerStamp = function () {
        this._connection.send(new session_1.default.AdjustByServerStampReq());
    };
    return TimeCalibration;
}());
exports.default = TimeCalibration;


//# sourceURL=webpack://MTDX/./src/core/login/timeCalibration.ts?